/*******************************************************************************
 * Package: com.hngtrust.iir.industry.controller
 * Type:    IndustryController
 * Date:    2021-12-7 15:55
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.industry.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.IndustryVO;
import com.hngtrust.iir.base.entity.IndustrySw;
import com.hngtrust.iir.base.service.IndustrySwService;
import com.hngtrust.iir.company.dto.ListKeyDTO;
import com.hngtrust.iir.company.entity.CompIndustry;
import com.hngtrust.iir.company.entity.CompKey;
import com.hngtrust.iir.company.entity.IndustryKey;
import com.hngtrust.iir.company.service.CompIndustryService;
import com.hngtrust.iir.company.service.CompKeyService;
import com.hngtrust.iir.company.service.IndustryKeyService;
import com.hngtrust.iir.exception.IirErrorType;
import com.hngtrust.iir.exception.IirException;
import com.hngtrust.iir.industry.controller.vo.AddCompanyReqVO;
import com.hngtrust.iir.industry.controller.vo.AddKeyReqVO;
import com.hngtrust.iir.industry.controller.vo.EditCompanyKeyReqVO;
import com.hngtrust.iir.industry.controller.vo.EditIndustryReqVO;
import com.hngtrust.iir.industry.controller.vo.ListCompKeyRespVO;
import com.hngtrust.iir.industry.controller.vo.ListIndicatorMapRespVO;
import com.hngtrust.iir.industry.controller.vo.ListIndicatorRespVO;
import com.hngtrust.iir.industry.controller.vo.ListKeyRespVO;
import com.hngtrust.iir.industry.controller.vo.ListPerformanceMapRespVO;
import com.hngtrust.iir.industry.service.IndustryDataService;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.util.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 行业分析管理
 * @author Chentianyong
 * @date 2021-12-7 15:55
 */
@Slf4j
@Api(tags = "行业分析管理")
@RestController
@RequestMapping("/industry")
public class IndustryController {
    
    @Autowired
    private IndustrySwService industrySwService;
    
    @Autowired
    private CompIndustryService compIndustryService;
    
    @Autowired
    private CompKeyService compKeyService;
    
    @Autowired
    private IndustryKeyService industryKeyService;
    
    @Autowired
    private IndustryDataService industryDataService;
    
    
    /**
     * 主键生成器
     */
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    /**
     * 行业新增
     */
    @ApiOperation(value = "新增行业", notes = "新增行业")
    @PostMapping("/add")
    public ResponseInfo<Object> addIndustry(
            @ApiParam(value = "新增行业") @Valid @RequestBody EditIndustryReqVO req) {
        log.info("IndustryController.addIndustry start...");
        
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        IndustrySw industrySw = new IndustrySw();
        //生成编号
        String parentCode = req.getParentCode().substring(0,6);
        String maxCode = industrySwService.getMaxCode(parentCode);
        String code = parentCode + "01";
        if(!maxCode.isEmpty()){
            Integer curCode = Integer.valueOf(maxCode.substring(6));
            ++curCode;
            if(curCode > 0 && curCode < 10){
                code = parentCode + "0" + curCode.toString();
            }
            else if(curCode >= 10 && curCode < 99) {
                code = parentCode + curCode.toString();
            }
        }
    
        industrySw.setId(myIdGenerator.nextId());
        industrySw.setName(req.getName());
        industrySw.setCode(code);
        industrySw.setLevel(4);
        industrySw.setStatus(0);
        industrySw.setCreateTime(new Date());
        industrySw.setCreateUserId(loginUser.getId());
        industrySw.setUpdateTime(new Date());
        industrySw.setUpdateUserId(loginUser.getId());
        industrySwService.add(industrySw);
        
        return new ResponseInfo<>();
    }
    
    /**
     * 添加关联公司
     */
    @ApiOperation(value = "添加关联公司", notes = "添加关联公司")
    @PostMapping("/add-company")
    public ResponseInfo<Object> addCompany(
            @ApiParam(value = "添加关联公司") @Valid @RequestBody AddCompanyReqVO req) {
        log.info("IndustryController.addCompany start...");
        
        CompIndustry compIndustry = new CompIndustry();
        compIndustry.setIndustryId(req.getIndustryId());
        compIndustry.setCompId(req.getCompId());
        List<CompIndustry> list  = compIndustryService.queryList(compIndustry);
        if(!list.isEmpty()){
            throw new IirException(IirErrorType.DATA_EXIST);
        }

        BeanUtils.copyProperties(req,compIndustry);
    
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        compIndustry.setId(myIdGenerator.nextId());
        compIndustry.setCreateTime(new Date());
        compIndustry.setCreateUserId(loginUser.getId());
        compIndustry.setUpdateTime(new Date());
        compIndustry.setUpdateUserId(loginUser.getId());
        compIndustryService.add(compIndustry);
        
        return new ResponseInfo<>();
    }
    
    /**
     * 修改关联公司
     */
    @ApiOperation(value = "修改关联公司", notes = "修改关联公司")
    @PostMapping("/edit-company")
    public ResponseInfo<Object> editCompany(
            @ApiParam(value = "修改关联公司") @Valid @RequestBody AddCompanyReqVO req) {
        log.info("IndustryController.editCompany start...");
        
        if(null == req.getId()){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        CompIndustry compIndustry = new CompIndustry();
        BeanUtils.copyProperties(req,compIndustry);

        compIndustry.setUpdateTime(new Date());
        compIndustry.setUpdateUserId(loginUser.getId());
        compIndustryService.modifyById(compIndustry);
        //再删除关键词
        compKeyService.deleteByCompInduId(req.getId());
        
        return new ResponseInfo<>();
    }
    
    /**
     * 删除关联公司和对应关键词
     */
    @ApiOperation(value = "删除关联公司和对应关键词", notes = "删除关联公司和对应关键词,传id")
    @PostMapping("/del-company")
    public ResponseInfo<Object> delCompany(
            @ApiParam(value = "删除关联公司和对应关键词") @Valid @RequestBody AddCompanyReqVO req) {
        log.info("IndustryController.delCompany start...");
        
        if(null == req.getId()){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        compIndustryService.deleteById(req.getId());
        //再删除关键词
        compKeyService.deleteByCompInduId(req.getId());
        
        return new ResponseInfo<>();
    }
    
    /**
     * 删除公司对应的关键词
     */
    @ApiOperation(value = "删除公司对应的关键词", notes = "删除公司对应的关键词,传id")
    @PostMapping("/del-company-key")
    public ResponseInfo<Object> delCompanyKey(
            @ApiParam(value = "删除公司对应的关键词") @Valid @RequestBody AddCompanyReqVO req) {
        log.info("IndustryController.delCompanyKey start...");
        
        if(null == req.getId()){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        //删除关键词
        compKeyService.deleteByCompInduId(req.getId());
        
        return new ResponseInfo<>();
    }
    
    /**
     * 编辑公司对应的关键词
     */
    @ApiOperation(value = "编辑公司对应的关键词", notes = "编辑公司对应的关键词")
    @PostMapping("/edit-company-key")
    public ResponseInfo<Object> editCompanyKey(
            @ApiParam(value = "编辑公司对应的关键词") @Valid @RequestBody EditCompanyKeyReqVO req) {
        log.info("IndustryController.editCompanyKey start...");
        
        if(null == req.getId()){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        //删除关键词
        compKeyService.deleteByCompInduId(req.getId());
        if(null != req.getCheckKeys()){
            User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
            CompKey compKey = new CompKey();
            for(ListKeyDTO data : req.getCheckKeys()){
                compKey.setId(myIdGenerator.nextId());
                compKey.setCompInduId(req.getId());
                compKey.setKeyId(data.getKeyId());
                compKey.setCreateTime(new Date());
                compKey.setCreateUserId(loginUser.getId());
                compKey.setUpdateTime(new Date());
                compKey.setUpdateUserId(loginUser.getId());
                compKeyService.add(compKey);
            }
            
        }
        
        return new ResponseInfo<>();
    }
    
    /**
     * 添加关键词
     */
    @ApiOperation(value = "添加关键词", notes = "添加关键词")
    @PostMapping("/add-key")
    public ResponseInfo<Object> addKey(
            @ApiParam(value = "添加关键词") @Valid @RequestBody AddKeyReqVO req) {
        log.info("IndustryController.addKey start...");
    
        IndustryKey industryKey = new IndustryKey();
        industryKey.setIndustryId(req.getIndustryId());
        industryKey.setKeyName(req.getKeyName());
        List<IndustryKey> list = industryKeyService.queryList(industryKey);
        if(!list.isEmpty()){
            throw new IirException(IirErrorType.DATA_EXIST);
        }
        
        BeanUtils.copyProperties(req,industryKey);
        
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        industryKey.setId(myIdGenerator.nextId());
        industryKey.setCreateTime(new Date());
        industryKey.setCreateUserId(loginUser.getId());
        industryKey.setUpdateTime(new Date());
        industryKey.setUpdateUserId(loginUser.getId());
        industryKeyService.add(industryKey);
        
        return new ResponseInfo<>();
    }
    
    /**
     * 修改关键词
     */
    @ApiOperation(value = "修改关键词", notes = "修改关键词")
    @PostMapping("/edit-key")
    public ResponseInfo<Object> editKey(
            @ApiParam(value = "修改关键词") @Valid @RequestBody AddKeyReqVO req) {
        log.info("IndustryController.editKey start...");
        if(null == req.getId()){
            throw new IirException(IirErrorType.INTFACE_PARAMS_ERROR,"ID");
        }
        
        IndustryKey industryKey = new IndustryKey();
        industryKey.setIndustryId(req.getIndustryId());
        industryKey.setKeyName(req.getKeyName());
        List<IndustryKey> list = industryKeyService.queryList(industryKey);
        if(!list.isEmpty()){
            throw new IirException(IirErrorType.DATA_EXIST);
        }
        
        BeanUtils.copyProperties(req,industryKey);
        
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        industryKey.setUpdateTime(new Date());
        industryKey.setUpdateUserId(loginUser.getId());
        industryKeyService.modifyById(industryKey);
        
        return new ResponseInfo<>();
    }
    
    /**
     * 根据4级行业查询对应的公司和关键词
     */
    @ApiOperation(value = "根据4级行业查询对应的公司和关键词", notes = "根据4级行业查询对应的公司和关键词,传行业id")
    @PostMapping("/list-comp-key")
    public ResponseInfo<List<ListCompKeyRespVO>> listCompKey(
            @ApiParam(value = "根据4级行业查询对应的公司和关键词") @Valid @RequestBody IndustryVO req) {
        log.info("IndustryController.listCompKey start...");
    
        CompIndustry compIndustry = new CompIndustry();
        compIndustry.setIndustryId(req.getId());
        List<CompIndustry> list = compIndustryService.queryList(compIndustry);
        List<ListCompKeyRespVO> resp = new ArrayList<>();
        ListUtil<ListCompKeyRespVO> listUtil = BeanUtils.instantiateClass(ListUtil.class);
        listUtil.copyList(list,resp,ListCompKeyRespVO.class);
        
        List<ListKeyDTO> keyList = compIndustryService.queryKeys(compIndustry);
        for(ListKeyDTO key : keyList){
            for(ListCompKeyRespVO data : resp){
                if(key.getCompInduId().equals(data.getId())){
                    if(null == data.getKeys()){
                        data.setKeys(new ArrayList<>());
                    }
                    data.getKeys().add(key);
                }
            }
        }
        
        ResponseInfo<List<ListCompKeyRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 根据公司关系获取关键词
     */
    @ApiOperation(value = "根据公司关系获取关键词", notes = "根据公司关系获取关键词,传id和industryId")
    @PostMapping("/list-key")
    public ResponseInfo<ListKeyRespVO> listKey(
            @ApiParam(value = "根据公司关系获取关键词") @Valid @RequestBody AddCompanyReqVO req) {
        log.info("IndustryController.listKey start...");
    
        IndustryKey industryKey = new IndustryKey();
        industryKey.setIndustryId(req.getIndustryId());
        List<IndustryKey> industryKeys = industryKeyService.queryList(industryKey);
        ListKeyRespVO resp = new ListKeyRespVO();
        for(IndustryKey obj : industryKeys){
            ListKeyDTO data = new ListKeyDTO();
            data.setKeyId(obj.getId());
            data.setName(obj.getKeyName());
            if(null == resp.getKeys()){
                resp.setKeys(new ArrayList<>());
            }
            resp.getKeys().add(data);
        }
        
        //再获取已选数据
        CompIndustry compIndustry = new CompIndustry();
        compIndustry.setId(req.getId());
        List<ListKeyDTO> keyList = compIndustryService.queryKeys(compIndustry);
        if(!keyList.isEmpty()){
            resp.setCheckKeys(keyList);
        }
        
        ResponseInfo<ListKeyRespVO> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 行业经济指标图表
     */
    @ApiOperation(value = "行业经济指标图表", notes = "行业经济指标图表")
    @PostMapping("/list-indicator-map")
    public ResponseInfo<List<ListIndicatorMapRespVO>> listIndicatorMap(
            @ApiParam(value = "行业经济指标图表") @Valid @RequestBody IndustryVO req) {
        log.info("IndustryController.listIndicatorMap start...");
    
        List<ListIndicatorMapRespVO> resp = industryDataService.listIndicatorMap(req);
        
        ResponseInfo<List<ListIndicatorMapRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 业绩效评价标准图表
     */
    @ApiOperation(value = "企业绩效评价标准图表", notes = "企业绩效评价标准图表")
    @PostMapping("/list-performance-map")
    public ResponseInfo<List<ListPerformanceMapRespVO>> listPerformanceMap(
            @ApiParam(value = "企业绩效评价标准图表") @Valid @RequestBody IndustryVO req) {
        log.info("IndustryController.listPerformanceMap start...");
    
        List<ListPerformanceMapRespVO> resp = industryDataService.listPerformanceMap(req);
        
        ResponseInfo<List<ListPerformanceMapRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 行业经济指标列表
     */
    @ApiOperation(value = "行业经济指标列表", notes = "行业经济指标列表")
    @PostMapping("/list-indicator")
    public ResponseInfo<List<ListIndicatorRespVO>> listIndicator(
            @ApiParam(value = "行业经济指标列表") @Valid @RequestBody IndustryVO req) throws Exception {
        log.info("IndustryController.listIndicator start...");
        
        List<ListIndicatorRespVO> resp = industryDataService.listIndicator(req);
        
        ResponseInfo<List<ListIndicatorRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
    
    /**
     * 业绩效评价标准列表
     */
    @ApiOperation(value = "业绩效评价标准列表", notes = "业绩效评价标准列表")
    @PostMapping("/list-performance")
    public ResponseInfo<List<ListIndicatorRespVO>> listPerformance(
            @ApiParam(value = "业绩效评价标准列表") @Valid @RequestBody IndustryVO req) throws Exception {
        log.info("IndustryController.listPerformance start...");
        
        List<ListIndicatorRespVO> resp = industryDataService.listPerformance(req);
        
        ResponseInfo<List<ListIndicatorRespVO>> data = new ResponseInfo<>();
        data.setData(resp);
        return data;
    }
}
